package n_02;

/**
 * @author ChengMiao
 * @title: MyDemo
 * @projectName leet-code-record
 * @description: TODO
 * @date 2023/3/27 16:08
 */
public class MyDemo {
    public static void main(String[] args) {
        ListNode n1  = new ListNode(2,new ListNode(4,new ListNode(3)));
        ListNode n2  = new ListNode(5,new ListNode(6,new ListNode(4)));


        ListNode listNode = addTwoNumbers(n1, n2);
        System.out.println(listNode);
    }

    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        // [2,4,3]
        // [5,6,4]
        // [7,0,8]


        long l = 0 ;
        long k = 0;
        l =  getNum(l1,l,1);
        k =  getNum(l2,k,1);

        long sum = l + k;

        ListNode n = new ListNode();
        char[] chars = String.valueOf(sum).toCharArray();

        ListNode listNode = toNode(new ListNode(), chars, chars.length-1);


        return listNode;

    }



    public static ListNode toNode(ListNode n,char[] chars,int index){

        if(index>=0){
            n = new ListNode();
            n.val = Integer.parseInt(String.valueOf(chars[index]));
            n.next = toNode(null,chars,index-1);

        }



        return n;
    }


    public static long getNum(ListNode n,long sum,long i){


        long k = n.val * i ;
        sum = sum + k;
        i = i *10;

        if(n.next!=null){
            return getNum(n.next,sum,i);
        }

        return sum;

    }

}
